#页面父类：
#通用的属性：base_url driver
#通用的操作：定位元素、操作元素
import time,os,sys,time
sys.path.append(os.getcwd())

from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

class PageBase(object):
    def __init__(self,driver,base_url):
        self.driver=driver
        self.base_url=base_url

    #通用操作：打开pathurl
    def open(self,path_url):
        self.driver.get(self.base_url+path_url)

    #通用操作：定位元素
    def find_ele(self,loc,isGaoLian=False):
        by=loc[0]
        value=loc[1]
        ele=self.driver.find_element(by,value)
        if isGaoLian:
            self.driver.execute_script("arguments[0].setAttribute('style','color:red; background-color:green; font-size:25px; ')", ele)
        return ele

    def find_eles(self,loc):
        by=loc[0]
        value=loc[1]
        return self.driver.find_elements(by,value)

    #适合控件：按钮类
    #功能：点击某按钮
    def click(self,loc):
        ele=self.find_ele(loc)
        ele.click()

    #适合控件：文本框、密码框、多行文本框
    #功能：输入数据
    def input(self,loc,data):
        self.find_ele(loc,True).send_keys(data)

    # 适合控件：文本框、密码框、多行文本框
    # 功能：先清空原有输入数据再输入新数据，不会失去焦点。
    def input1(self, loc, data):
        ele=self.find_ele(loc)
        ele.send_keys(Keys.CONTROL,"a")
        ele.send_keys(Keys.BACK_SPACE)

        ele.send_keys(data)

    #适合控件：单选按钮
    #功能：选中某选项
    def click_radio(self,nameAttr,valueAttr):
        #<input type="radio" name="sex" value="M" />
        ele=self.driver.find_element_by_css_selector(f"input[type='radio'][name='{nameAttr}'][value='{valueAttr}']")
        ele.click()

    #适合控件：复选框
    #功能：选中一些选项
    def click_checkbox(self,nameAttr,targetValues):
        #<input type="checkbox" name="aihao"
        allOption=self.driver.find_elements_by_css_selector(f"input[type='checkbox'][name='{nameAttr}']")
        for x in allOption:
            valueAttr=x.get_attribute("value") #"dalanqiu" "tizuqiu"
            status = x.is_selected()
            if valueAttr in targetValues:
                if not status:
                    x.click()
            else:
                if status:
                    x.click()

    #适合控件：下拉列表
    #功能：选中某选项
    def click_select(self,loc_select,option,type="text"):
        ele_select = self.find_ele(loc_select)
        select = Select(ele_select)
        if type=="text":
            select.select_by_visible_text(option)
        elif type=="index":
            select.select_by_index(option)
        elif type=="value":
            select.select_by_value(option)

    #适合控件：日期控件
    #功能：输入某日期
    def input_date(self,loc,riqi):
        ele=self.find_ele(loc)
        self.driver.execute_script("arguments[0].value=arguments[1]",ele,riqi)


    #适合控件：文件上传
    #功能：上传1个或多个文件/目录。方法1：不通用
    def uploadFile1(self,loc,filePath):#"C:/A/1.png" "C:/A/1.png,D:/A/2.png" ""
        if filePath=="" or filePath==None:
            return
        ele=self.find_ele(loc)
        newFile=filePath.replace(",","\n")
        ele.send_keys(newFile)

    #适合控件：文件上传
    #功能：上传1个或多个文件/目录。方法2：通用
    def uploadFile2(self,loc,filePath):#"C:/A/1.png" "C:/A/1.png,D:/A/2.png" ""
        if filePath=="" or filePath==None:
            return

        ele = self.find_ele(loc)
        ActionChains(self.driver).click(ele).perform()
        time.sleep(2)

        newFile = ""
        a = filePath.split(",")
        for x in a:
            newFile += '"' + x + '" '
        newFile = newFile.replace("/", "\\")
        os.system(f'C:/B/uploadFile.exe {newFile}')


    #以下这些是JS常见操作的封装

    #通过JS代码输入数据
    def js_input(self,loc,data):
        ele=self.find_ele(loc)
        self.driver.execute_script("arguments[0].value=arguments[1]",ele,data)

    #通过JS代码点击
    def js_click(self,loc):
        ele=self.find_ele(loc)
        self.driver.execute_script("arguments[0].click()", ele)

    # 通过JS代码修改某元素的某属性的值
    def js_setEleAttr(self,loc,attrName,attrValue):
        ele=self.find_ele(loc)
        self.driver.execute_script("arguments[0].setAttribute(arguments[1],arguments[2])",ele,attrName,attrValue)

    #通过JS代码来删除某元素的某属性
    def js_delEleAttr(self,loc,attrName):
        ele=self.find_ele(loc)
        self.driver.execute_script("arguments[0].removeAttribute(arguments[1])",ele,attrName)


    #通过JS来修改某元素的CSS样式/高亮显示元素
    def js_setEleCssStyle(self,loc,css_style):#”color:red; display:block;”
        ele=self.find_ele(loc)
        self.driver.execute_script("arguments[0].setAttribute('style',arguments[1])",ele,css_style)

    #获取某元素的文本内容/某属性。默认是获取的是文本内容
    def getEleTextOrAttr(self,loc,attrName="text"):
        ele=self.find_ele(loc)
        if attrName=="text":
            return ele.text
        else:
            return ele.get_attribute(attrName)

    #以下封装是鼠标相关操作
    #鼠标单击某元素
    def mouse_click(self,loc):
        ele=self.find_ele(loc)
        ActionChains(self.driver).click(ele).perform()

        #鼠标双击某元素
    def mouse_doubleclick(self,loc):
        ele=self.find_ele(loc)
        ActionChains(self.driver).double_click(ele).perform()

    #鼠标悬停在某元素上
    def mouse_moveToEle(self,loc):
        ele=self.find_ele(loc)
        ActionChains(self.driver).move_to_element(ele).perform()


    #把当前web页面进行截图保存
    def savePng(self,serviceName):
        currTime=time.strftime("%Y%m%d%H%M%S") #"20250304165401"
        imgName=f"{serviceName}_{currTime}"#"登录_20250304165401"
        self.driver.get_screenshot_as_file(f"screen/{imgName}.png")
        return imgName
